package controller.class1;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.class1.dao.Class1Dao;
import model.class1.dto.Class1Dto;
import model.class1.dto.ResistDto;
import model.member.dao.MemberDao;
import model.member.dto.MemberDto;
import model.recipe.dao.RecipeDao;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

public class Class1Controller extends MultiActionController {
	private Map<String,Object> map;
	private HttpSession session;
	private Class1Dto cdto;
	
	private Class1Dao class1Dao;
	public void setClass1Dao(Class1Dao class1Dao) {
		this.class1Dao = class1Dao;
	}
	private RecipeDao recipeDao;
	public void setRecipeDao(RecipeDao recipeDao) {
		this.recipeDao = recipeDao;
	}
	private MemberDao memberDao;
	public void setMemberDao(MemberDao memberDao) {
		this.memberDao = memberDao;
	}
///////////////////////////////////
	// 우창 작업
	public ModelAndView class1_write(HttpServletRequest request, HttpServletResponse response) throws Exception{
		session = request.getSession();
		String userid = (String)session.getAttribute("userid"); 
		if( userid == null){
			return new ModelAndView("redirect:/member/login.jsp");
		}
		map = new HashMap<String, Object>();
		map.put("recipeList", recipeDao.myListRateAll(userid));		
		return new ModelAndView("/class1/class1_write.jsp","map",map);
	}
	
	public ModelAndView recipe_info(HttpServletRequest request, HttpServletResponse response) throws Exception{		
		int recipe_idx = Integer.parseInt(request.getParameter("recipe_idx"));
		map = new HashMap<String, Object>();
		map.put("recipeInfo", recipeDao.recipeInfo(recipe_idx));		
		return new ModelAndView("/class1/class1_write_info.jsp","map",map);
	}
	
	public ModelAndView class1_date_list(HttpServletRequest request, HttpServletResponse response) throws Exception{
		map = new HashMap<String, Object>();
		String date = request.getParameter("date");
		map.put("classDateList",class1Dao.class1DateList(date));		
		return new ModelAndView("/class1/class1_write_calendar.jsp","map",map);
	}

	public String class1_insert(HttpServletRequest request, HttpServletResponse response) throws Exception{
		session = request.getSession();
		String userid = (String)session.getAttribute("userid");
		if( !userid.equals(request.getParameter("userid"))){
			return "redirect:/member/login.jsp";
		}
		cdto = new Class1Dto();
		cdto.setUserid(userid);
		cdto.setSubject(request.getParameter("subject"));
		cdto.setClass_date(request.getParameter("class_date"));
		cdto.setContent(request.getParameter("content"));
		cdto.setRecipe_idx1(Integer.parseInt(request.getParameter("recipe_idx1")));
		String recipe_idx2 = request.getParameter("recipe_idx2");
		if( recipe_idx2 != null ){
			cdto.setRecipe_idx2(Integer.parseInt(recipe_idx2));
		}		
		class1Dao.class1Insert(cdto);		
		return "redirect:/class1/class1_list.do";
	}
	
	// 가람

	// my_resist페이지에서 수강신청 취소 class1_resist_delete.do
	public String class1_resist_delete(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		int r_idx = Integer.parseInt(request.getParameter("r_idx"));
		
		HttpSession session = request.getSession();
		String userid = (String) session.getAttribute("userid");
		ResistDto rDto = class1Dao.class1ResistList(r_idx);
		
			if (rDto.getUserid().equals(userid)) 
			{
				System.out.println("delete resist");
				class1Dao.class1ResistDelete(r_idx);
			}
			return "redirect:/class1/class1_list.do";
		}

	
	//my Resist pageㄱㄱ
	public ModelAndView class1_my_resist(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		int idx= Integer.parseInt(request.getParameter("idx"));
		int r_idx = Integer.parseInt(request.getParameter("r_idx"));
		System.out.println(r_idx);
		HttpSession session = request.getSession();
		String userid = (String) session.getAttribute("userid");
		Class1Dto cDto = class1Dao.class1Detail(idx);
		MemberDto mDto = memberDao.memberDetail(userid);
		Class1Dto rDto = class1Dao.class1ResistDetail(r_idx);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("cDto", cDto);
		map.put("mDto", mDto);
		map.put("rDto", rDto);
		return new ModelAndView("/class1/class1_my_resist.jsp", "map", map);
	}
	
	// 관리자가 가격 설정해서 class1_admin_update.do
	public String class1_admin_update(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		int idx = Integer.parseInt(request.getParameter("idx"));
		HttpSession session = request.getSession();
		String userid = (String) session.getAttribute("userid");
		if (userid.equals("admin")) {// 관리자일 경우에만 ㄱㄱ
			Class1Dto dto = new Class1Dto();
			dto.setIdx(idx);
			dto.setPrice(Integer.parseInt(request.getParameter("price")));
			dto.setState("1");
			class1Dao.class1AdminPriceUpdate(dto);
		} else {
			return "redirect:/member/login.jsp";
		}
		return "redirect:/admin/admin_class1_list.jsp";
	}

	public String class1_delete(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		int idx = Integer.parseInt(request.getParameter("idx"));
		Class1Dto dto = class1Dao.class1Detail(idx);
		// 본인 게시물or관리자만 삭제할 수 있도록 세션 검사
		HttpSession session = request.getSession();
		String userid = (String) session.getAttribute("userid");
		if (userid.equals(dto.getUserid()) || userid.equals("admin")) {
			if (dto.getCnt() == 0) {
				class1Dao.class1Delete(idx);
			} else {
				// //////메세지창 띄워주세요 //
				System.out.println("수강생이 있을 경우!");
			}
		}
		return "redirect:/class1/class1_list.do";
	}

	public String class1_update(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		int idx = Integer.parseInt(request.getParameter("idx"));
		Class1Dto dto = new Class1Dto();
		dto.setIdx(idx);
		dto.setSubject(request.getParameter("subject"));
		dto.setContent(request.getParameter("content"));
		class1Dao.class1Update(dto);
		return "redirect:/class1/class1_list.do";
	}

	// 강좌 신청하는 class1_resist_insert.do
	public ModelAndView class1_resist_insert(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		HttpSession session = request.getSession();
		String userid = (String) session.getAttribute("userid");
		int idx = Integer.parseInt(request.getParameter("idx"));
		Class1Dto cDto = new Class1Dto();
		cDto.setM_userid(userid);
		cDto.setIdx(idx);

		class1Dao.class1ResistInsert(cDto);

		MemberDto dto = memberDao.memberDetail(userid);
		map = new HashMap<String, Object>();
		map.put("dto", dto);
		return new ModelAndView("/member/memberDetail.do?userid="+userid, "map", map);

	}

	// 강좌 신청할때
	public ModelAndView class1_resist(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		int idx = Integer.parseInt(request.getParameter("idx"));
		HttpSession session = request.getSession();
		String userid = (String) session.getAttribute("userid");
		Class1Dto cDto = class1Dao.class1Detail(idx);
		MemberDto mDto = memberDao.memberDetail(userid);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("cDto", cDto);
		map.put("mDto", mDto);
		return new ModelAndView("/class1/class1_resist.jsp", "map", map);
	}

	// 자기가 쓴 강좌게시물 수정
	public ModelAndView class1_edit(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		int idx = Integer.parseInt(request.getParameter("idx"));
		Class1Dto dto = class1Dao.class1Detail(idx);

		return new ModelAndView("/class1/class1_edit.jsp", "dto", dto);

	}

	// 일반유저가 보는 게시물 디테일
	public ModelAndView class1_detail(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// 게시물 번호로 dto 조회
		int idx = Integer.parseInt(request.getParameter("idx"));
		// 조회수 증가처리
		class1Dao.hitUp(idx);
		Class1Dto dto = class1Dao.class1Detail(idx);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("dto", dto);
		return new ModelAndView("/class1/class1_detail.jsp", "map", map);

	}

	// 강좌 게시물 리스트
	public ModelAndView class1_list(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		HttpSession session = request.getSession();
		String userid = (String) session.getAttribute("userid");
		// 검색옵션
		String state = request.getParameter("state_option");

		if (userid == null) {
			state = "1";
		}

		else if (userid.equals("admin")) {
			if (state == null)
				state = "multi";
		} else {
			state = "1";
		}
		// 페이지 정보 설정
		// 페이지 네비게이션 문자열
		// 현재 페이지 번호
		Map<String, Object> map = new HashMap<String, Object>();
		List<Class1Dto> list = class1Dao.class1List(state);
		List<Class1Dto> myList = class1Dao.class1MyList(userid);
		List<Class1Dto> myOrderList = class1Dao.class1MyOrderList(userid);
		map.put("list", list);
		map.put("state", state);
		map.put("myList", myList);
		map.put("myOrderList", myOrderList);
		return new ModelAndView("/class1/class1_list.jsp", "map", map);

	}
}
